//
//  RegisterViewController.m
//  iShanggang
//
//  Created by aiShangGang on 2017/3/31.
//  Copyright © 2017年 aishanggang. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "LoginViewController.h"
#import "BOAssistor.h"
#import "BONoticeBar.h"
#import "NetWorkRequest.h"
#import "BaseWebViewController.h"
#import "UIColor+Hex.h"
@interface RegisterViewController () {
    UITextField *_phoneField;
    UITextField *_codeField;
//    UITextField *_passwordField;
}

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatUI];
    [self layoutNavItem];
    // 手机验证码登录通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didClickProtocal) name:@"didClickProtocal" object:nil];
    //注册的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registerAccount) name:@"registerAccount" object:nil];
}

- (void)layoutNavItem {
    [self ISG_NavigationBarDefaultBackButtonAndNavigationTitle:@"注册"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [BOAssistor setDefaultCountDownValue];
}

#pragma mark - creatUI

- (void)creatUI {
    self.view.backgroundColor = cCommonHeaderGrayColor;
    
    RegisterView *registerV = [[RegisterView alloc]initWithFrame:CGRectMake(0, 0, Width, Height-64)];
    _phoneField = registerV.accountTextField;
    _codeField = registerV.codeTextField;
//    _passwordField = registerV.passwordTextField;
    [self.view addSubview:registerV];
    
    [registerV.accountTextField becomeFirstResponder];
}

#pragma mark - 点击事件
- (void)didClickLeftBarBtn:(UIBarButtonItem *)leftBarBtn {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didClickRightBarBtn:(UIBarButtonItem *)rightBarBtn {
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}

- (void)registerAccount {
    [self.view endEditing:YES];
    if ( _phoneField.text.length == 0) {
        self.noticeText = @"手机号码不能为空";
        return;
    }else if (_codeField.text.length == 0) {
        self.noticeText = @"验证码不能为空";
        return;
    }
//    else if (_passwordField.text.length < 4) {
//        self.noticeText = @"密码长度不能小于4位";
//        return;
//    }else if (_passwordField.text.length > 20) {
//        self.noticeText = @"密码长度不能超过20位";
//        return;
//    }
    else {
        __weak typeof(self) weakSelf = self;
        NSDictionary *pramas = [NSDictionary dictionaryWithObjectsAndKeys:_phoneField.text,@"username",_codeField.text,@"code",@"123456",@"password", nil];
        
        [NetWorkRequest postNetRequestData:@"registerURL" parmeters:pramas response:^(NSDictionary *response) {
            [weakSelf progressHUDHideImmediately];
            if ([response[@"status"]integerValue] == 0) {
                
//                [ISGUserInfo sharedUserInfo].sessionID = result[@"sessionId"];
//                [ISGUserInfo sharedUserInfo].bindPhoneFlag = YES;
//                [[ISGUserInfo sharedUserInfo] saveUserInfo];
                
                [self progressHUDShowWithCompleteText:@"注册成功" isSucceed:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccessNoti" object:nil];
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }else {
                [[BONoticeBar defaultBar] setNoticeText:response[@"msg"]];
            }
        } netError:^(NSError *error) {
            [weakSelf progressHUDShowWithText:@"注册失败"];
        }];
    }
}

#pragma mark - notification
- (void)didClickProtocal {
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"agreement.html" withExtension:nil];
    BaseWebViewController *protocalVC = [[BaseWebViewController alloc] initWithUrl:url];
    [self.navigationController pushViewController:protocalVC animated:YES];
}

@end
